import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//使用数组实现
public class Solution {
    public static int[] intersection(int[] nums1, int[] nums2) {
        int[] hash1 = new int[1001];
        int[] hash2 = new int[1001];
        for (int i : nums1) {
            hash1[i] = 1;
        }
        for (int i : nums2) {
            hash2[i] = 1;
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 1001; i++) {
            if (hash1[i] != 0 && hash2[i] != 0) {
                list.add(i);
            }
        }
        int index = 0;
        int[] result = new int[list.size()];
        for (int i : list) {
            result[index++] = i;
        }
       return result;
    }

    public static void main(String[] args) {
        int[] nums1 = {1,2,3,8,9};
        int[] nums2 = {1,5,8,7,5};
        int[] ret = intersection(nums1,nums2);
        System.out.println(Arrays.toString(ret));
    }
}